package com.znufe.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.znufe.exam.domain.ExamPaper;
import com.znufe.exam.domain.ExamPaperRule;
import com.znufe.exam.domain.other.KeyValue;
import com.znufe.exam.repository.ExamRuleMapper;
import com.znufe.exam.repository.QuestionMapper;
import com.znufe.exam.service.ExamPaperRuleService;
import com.znufe.exam.service.QuestionService;
import com.znufe.exam.service.SubjectService;
import com.znufe.exam.service.TextContentService;
import com.znufe.exam.utility.DateTimeUtil;
import com.znufe.exam.utility.ModelMapperSingle;
import com.znufe.exam.viewmodel.admin.exam.ExamRuleRequestVM;
import com.znufe.exam.viewmodel.student.dashboard.PaperFilter;
import com.znufe.exam.viewmodel.student.dashboard.PaperInfo;
import com.znufe.exam.viewmodel.student.exam.ExamPaperPageVM;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExamPaperRuleServiceImpl extends BaseServiceImpl<ExamPaper> implements ExamPaperRuleService {

    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();
    private final ExamRuleMapper examPrincipleMapper;
    private final QuestionMapper questionMapper;
    private final TextContentService textContentService;
    private final QuestionService questionService;
    private final SubjectService subjectService;

    @Autowired
    public ExamPaperRuleServiceImpl(ExamRuleMapper examPrincipleMapper,
                                    QuestionMapper questionMapper,
                                    TextContentService textContentService,
                                    QuestionService questionService,
                                    SubjectService subjectService) {
        super(examPrincipleMapper);
        this.examPrincipleMapper = examPrincipleMapper;
        this.questionMapper = questionMapper;
        this.textContentService = textContentService;
        this.questionService = questionService;
        this.subjectService = subjectService;
    }

    @Override
    public PageInfo<ExamPaperRule> page(ExamRuleRequestVM RuleVM) {
        return PageHelper.startPage(RuleVM.getPageNum(), RuleVM.getPageSize(),
                "id desc").doSelectPageInfo(() ->
                examPrincipleMapper.page(RuleVM));
    }

    @Override
    public PageInfo<ExamPaperRule> taskExamPage(ExamRuleRequestVM requestVM) {
        return PageHelper.startPage(requestVM.getPageNum(), requestVM.getPageSize(),
                "id desc").doSelectPageInfo(() ->
                examPrincipleMapper.taskExamPage(requestVM));
    }

    @Override
    public PageInfo<ExamPaperRule> studentPage(ExamPaperPageVM requestVM) {
        return PageHelper.startPage(requestVM.getPageNum(), requestVM.getPageSize(),
                "id desc").doSelectPageInfo(() ->
                examPrincipleMapper.studentPage(requestVM));
    }

    @Override
    public List<PaperInfo> indexPaper(PaperFilter paperFilter) {
        return examPrincipleMapper.indexPaper(paperFilter);
    }

    @Override
    public Integer selectAllCount() {
        return examPrincipleMapper.selectAllCount();
    }

    @Override
    public List<Integer> selectMothCount() {
        Date startTime = DateTimeUtil.getMonthStartDay();
        Date endTime = DateTimeUtil.getMonthEndDay();
        List<KeyValue> mouthCount = examPrincipleMapper.selectCountByDate(startTime, endTime);
        List<String> mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat();
        return mothStartToNowFormat.stream().map(md -> {
            KeyValue keyValue = mouthCount.stream().filter(kv -> kv.getName().equals(md)).
                    findAny().orElse(null);
            return null == keyValue ? 0 : keyValue.getValue();
        }).collect(Collectors.toList());
    }

    @Override
    public List<ExamPaper> getExamPaperListById(List<Integer> ids) {
        return examPrincipleMapper.getExamPaperListById(ids);
    }

    @Override
    public List<ExamPaper> getAllExamPaperListById(List<Integer> ids) {
        return examPrincipleMapper.getAllExamPaperListById(ids);
    }
}
